package com.andamios.sesamo.model;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Set;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.UserDetails;

/**
 *
 * @author carlos
 */
public class User implements UserDetails  {
	private String username;
	private String password = "passValidated";
	private String status;
	private String autorizacion;
	private Integer id;
	private Set profiles;
	private String email;
	/** Creates a new instance of User */
	public User() {

	}

	public String getUsername(){ return username; }

	public void setUsername(String username){ this.username = username; }

	public String getPassword(){ return password; }



	public void setPassword(String password){ 
		this.password = "passValidated"; 
	}

	public boolean isEnabled() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isAccountNonExpired()  {
		return true;
	}

	public GrantedAuthority[] getAuthorities()  {
		 //GrantedAuthority[] grantedAuth = new GrantedAuthority[getProfiles().size()];
		 GrantedAuthority[] grantedAuth = new GrantedAuthority[1];
	     int index = 0;
	     /*Iterator it = getProfiles().iterator();
	       while(it.hasNext()){
	           String profile = (String) it.next();
	           grantedAuth[index] = new GrantedAuthorityImpl(profile.toUpperCase());	           
	           index++;
	          
	       }*/
	       grantedAuth[index] = new GrantedAuthorityImpl("ADMIN");
	       return grantedAuth;
	}

	public String getStatus(){ return status; }

	public void setStatus(String status){ this.status = status; }

	public void setAutorizacion(String autorizacion) {
		this.autorizacion = autorizacion;
	}
	public String getAutorizacion(){ return autorizacion; }

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Set getProfiles() {
		return profiles;
	}

	public void setProfiles(Set profiles) {
		this.profiles = profiles;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	private static String getMD5Hash(String data, String key) {

		try {

			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(data.getBytes());
			byte result[] = md5.digest(key.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < result.length; i++) {
				String s = Integer.toHexString(result[i]);
				int length = s.length();
				if (length >= 2) {
					sb.append(s.substring(length - 2, length));
				} else {
					sb.append("0");
					sb.append(s);
				}
			}
			return sb.toString();

		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}

}